﻿using Shared.Method;
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace TESTING
{
    class Program
    {
        //此测试类主要用于连接交换机，修改交换机配置
        static int s_offset = 0;

        static void Main(string[] args)
        {
            //string processName = "cmd.exe";
            // processName = processName.Substring(0, processName.IndexOf("."));
            BEGIN:

            string msg = string.Empty;
            using (SerialPort serial = PortBaseClass.GetNewSerialPort(9600, "COM5", 8, out msg))
            {
                //serial.Open();

                if (serial.IsOpen == false) { Console.WriteLine("打开端口失败"); return; }

                var recv = PortBaseClass.SerialPortSendAndRecv(serial, PortBaseClass.SCAN);

                if (recv.SequenceEqual(PortBaseClass.SCAN_ACK))
                {
                    PortBaseClass.SerialPortSendAndRecv(serial, string.Empty, "\r", out msg, 10);
                    Console.WriteLine(msg);
                }

                Console.WriteLine("按任意键结束");
                Console.ReadKey();
            }

            goto BEGIN;

            return;

            var tel = GetTelnet();

            //获取所有的端口编号
            if (tel == null)
            {
                Console.WriteLine("连接交换机失败");
                Console.ReadKey();
                return;
            }

            int port = 0;
            while (true)
            {
                //初始化端口vlan
                //if (EnterInterface("fastEthernet 1/0/" + port, tel))
                //{
                    //将初始vlan

                //}

                port++;

                if (port > 24) port = 1;

                //判断接口是否打开，若没有打开则直接跳过
                tel.SendAndRecv(string.Format("show interface fastEthernet 1/0/{0} status ", port), "copper", out msg);
                Console.WriteLine(msg);

                if (msg.Contains("LinkUp") == false) {continue; }

                //Thread.Sleep(2000);


                //if (EnterInterface("gigabitEthernet 1/0/26", tel) == false)
                if (EnterInterface("fastEthernet 1/0/" + port, tel) == false)
                {
                    Console.WriteLine("进入端口失败");

                    tel?.Close();
                    /*if (tel.Connected == false)*/
                    tel = GetTelnet();

                    if (tel == null) break;

                    continue;
                }

                //进入设置可以访问的端口号
                //tel.SendAndRecv("port isolation fa-forward-list 1/0/" + port, "#", out msg);
                //将端口设置到vlan中
                tel.SendAndRecv("switchport access vlan 1", "#", out msg);
                Console.WriteLine(msg);
                //连接光猫并点亮

                //EnterInterface("fastEthernet 1/0/" + port, tel, true);
                //SaveConfig(tel);

                ProcessClass.StartProcess("arp", out msg, new string[] { "-d"});
                Console.WriteLine(msg);

                Thread.Sleep(8000);


                //int pingOkTimes = 0;

                if (PingBaseClass.PingTest("192.168.1.213", "192.168.1.1") == false
                    && PingBaseClass.PingTest("192.168.1.213", "192.168.1.1") == false
                     && PingBaseClass.PingTest("192.168.1.213", "192.168.1.1") == false)
                {
                    Console.WriteLine("PING光猫失败");

                    goto TagOut;
                    //continue;
                }

                Console.WriteLine("PING光猫成功");

                //连接光猫并将光猫点亮
                s_offset++;

                if (s_offset == 1000) s_offset = 0;

                var telModem = YcdTelnetMethod.TelnetModem("192.168.1.213", "192.168.1.1", 32696+s_offset, 23, "admin", "1234");

                if (telModem != null)
                {
                    Console.WriteLine("登录光猫成功");
                    telModem.SendAndRecv("mpctl led on", "#",out msg);
                    Console.WriteLine(msg);
                    Thread.Sleep(5000);

                    telModem.SendAndRecv("mpctl led restore", "#", out msg);

                    Console.WriteLine(msg);

                    telModem.Send("exit");

                    Thread.Sleep(1000);
                    telModem.Close();
                }

            TagOut:

                tel.Send("\r\n");
                tel.SendAndRecv("switchport access vlan " + (100 + port), "#", out msg);
                Console.WriteLine(msg);
            }

            Console.ReadKey();
        }

        

        public static bool SaveConfig(YcdTelnetSecond tel)
        {
            string msg = string.Empty;
            tel.SendAndRecv("end", "#", out msg);
            Console.WriteLine(msg);

            var telRet= tel.SendAndRecv("copy running-config startup-config", "#", out msg,8000);
            Console.WriteLine(msg);

            return telRet;
        }

        public static YcdTelnetSecond GetTelnet()
        {
            //连接交换机
            YcdTelnetSecond tel = new YcdTelnetSecond("192.168.0.1", "192.168.0.213", 34580 + s_offset);
            if (tel.Connect() == false)
            {
                Console.WriteLine("连接失败");
                return null;
            }

            string msg = string.Empty;
            tel.SendAndRecv("ycd", ">#", out msg);

            Console.WriteLine(msg);

            tel.Send("\r\n");

            tel.SendAndRecv("enable", "password", out msg);

            Console.WriteLine(msg);

            tel.SendAndRecv("ycd", "#", out msg);

            Console.WriteLine(msg);

            return tel;
        }

        /// <summary>
        /// 进入某个接口
        /// </summary>
        /// <param name="portName"></param>
        /// <param name="tel"></param>
        /// <returns></returns>
        public static bool EnterInterface(string portName, YcdTelnetSecond tel/*, bool ifUpDown = false*/)
        {
            if (tel == null || tel.Connected == false) return false;

            //判断TELNET是否是控制模式
            string msg;
            if (tel.SendAndRecv("\r\n", "#", out msg) == false) return false;
            Console.WriteLine(msg);

            tel.SendAndRecv("end\r\n", "#", out msg);
            Console.WriteLine(msg);
            //if (tel.SendAndRecv("\r\n", "#", out msg) == false) return false;
            //Console.WriteLine(msg);

            tel.SendAndRecv("configure\r\n", "(config)#", out msg);
            Console.WriteLine(msg);
            //if (tel.SendAndRecv("\r\n", "#", out msg) == false) return false;
            //Console.WriteLine(msg);

            var telResult = tel.SendAndRecv("interface " + portName + "\r\n", "(config-if)#", out msg);
            Console.WriteLine(msg);
            //if (tel.SendAndRecv("\r\n", "#", out msg) == false) return false;
            //Console.WriteLine(msg);

            //if (ifUpDown)
            //{
            //    tel.SendAndRecv("shutdown", "#", out msg);
            //    Console.WriteLine(msg);

            //    tel.Send("\r\n");

            //    tel.SendAndRecv("no shutdown", "#", out msg);
            //    Console.WriteLine(msg);
            //}

            if (telResult == false)
            {
                Console.WriteLine("进入接口失败：" + portName);
                return false;
            }
            
            return true;
        }
    }
}
